package com.wgts.wgts.mvp.model.imp;

import com.wgts.wgts.mvp.model.ISearchActivityModel;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.NetService;
import com.wgts.wgts.rxjava.MVPResultSubscriber;
import com.wgts.wgts.tools.SharedpreferencesHelper;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;


/*
 *
 *
 *   data:2018/9/2710:07
 *
 */
public class SearchActivityModel implements ISearchActivityModel{
    private String history;
    private static final String History = "history";
    private Gson gson ;
    private List<String> historyList;

    private NetService netService;
    private SharedpreferencesHelper sharedpreferencesHelper;


    public SearchActivityModel(NetService netService, SharedpreferencesHelper sharedpreferencesHelper) {
        this.netService = netService;
        this.sharedpreferencesHelper = sharedpreferencesHelper;
        historyList = new ArrayList<>();
        gson = new Gson();
    }

    @Override
    public void hotSearchResult(HttpResultSubscriber<List<String>> subscriber) {
        netService.hotSearch(subscriber);
    }

    @Override
    public void WriteHistorySearch(String searctText) {
        saveHistorySearch(searctText);
    }

    @Override
    public void getHistorySearch(MVPResultSubscriber<List<String>> subscriber) {
        Observable.create(new Observable.OnSubscribe<List<String>>() {
            @Override
            public void call(Subscriber<? super List<String>> subscriber) {
                history = sharedpreferencesHelper.getString(History);
                if (history != null && !history.equals("")){
                    historyList = gson.fromJson(history, ArrayList.class);
                }
                subscriber.onNext(historyList);
            }
        }).subscribe(subscriber);
    }

    @Override
    public void clearHistorySearch(MVPResultSubscriber<List<String>> subscriber) {
        Observable.create(new Observable.OnSubscribe<List<String>>() {
            @Override
            public void call(Subscriber<? super List<String>> subscriber) {
                history = sharedpreferencesHelper.getString(History);
                if (history != null && !history.equals("")){
                    historyList = gson.fromJson(history, ArrayList.class);
                    historyList.clear();
                    String history = gson.toJson(historyList);
                    sharedpreferencesHelper.write(History, history);
                }
                subscriber.onNext(historyList);
            }
        }).subscribe(subscriber);
    }

    private void saveHistorySearch(String string){
        history = sharedpreferencesHelper.getString(History);
        if (history != null && !history.equals("")){
            historyList = gson.fromJson(history, ArrayList.class);
            if (historyList.size() >= 10){
                for (int i = 0; i < historyList.size(); i++){
                    if (historyList.get(i).equals(string)){
                        historyList.remove(i);
                        historyList.add(0, string);
                        String history = gson.toJson(historyList);
                        sharedpreferencesHelper.write(History, history);
                        return;
                    }
                }
                historyList.remove(9);
                historyList.add(0, string);
                String history = gson.toJson(historyList);
                sharedpreferencesHelper.write(History, history);
            }else {
                for (int i = 0; i < historyList.size(); i++){
                    if (historyList.get(i).equals(string)){
                        historyList.remove(i);
                        historyList.add(0, string);
                        String history = gson.toJson(historyList);
                        sharedpreferencesHelper.write(History, history);
                        return;
                    }
                }
                historyList.add(0, string);
                String history = gson.toJson(historyList);
                sharedpreferencesHelper.write(History, history);
            }

        }else {
            historyList.add(string);
            String history = gson.toJson(historyList);
            sharedpreferencesHelper.write(History, history);
        }
    }

}
